<template>
  <view class="ra_container">
    <nut-form>
      <nut-form-item label="代取人信息">

      </nut-form-item>
      <nut-form-item label="真实姓名" prop="name" required>
        <nut-input
            v-model="formData.name"
            class="nut-input-text"
            placeholder="请填写您的真实姓名"
            type="text"
            :border="false"
        />
      </nut-form-item>
      <nut-form-item label="联系方式" prop="phone" required>
        <nut-input
            v-model="formData.phone"
            class="nut-input-text"
            placeholder="请填写手机或座机号码"
            type="text"
            :border="false"
        />
      </nut-form-item>
      <nut-form-item label="身份证" prop="idCard" required>
        <nut-input
            v-model="formData.idCard"
            class="nut-input-text"
            placeholder="请填写您的身份证号码"
            type="text"
            :border="false"
        />
      </nut-form-item>
      <nut-form-item label="取货时间" required>
<!--        <nut-input-->
<!--            v-model="qhTime"-->
<!--            class="nut-input-text"-->
<!--            placeholder=""-->
<!--            type="text"-->
<!--            :border="false"-->
<!--            @focus="openDateChoose"-->
<!--        />-->
        <view class="st" @click="openDateChoose">{{ qhTime || '请选择取货时间段' }}</view>
      </nut-form-item>
    </nut-form>

    <view class="address-info">
      <view class="head">
        <text>承运单号：{{ orderInfo.billNo }}</text>
        <image class="car" src="../../assets/images/orderTracking/bgCar.png"></image>
      </view>
      <view class="sends">
        <view class="row">
          <view class="title_1">发</view>
          <view class="value" style="border-bottom: 1rpx solid #f5f5f5">
            <view class="name">{{ orderInfo.shipper }} {{ orderInfo.shipperTel }}</view>
            <view class="pos">{{ orderInfo.toFromAdd || '-' }}</view>
          </view>
        </view>
        <view class="row">
          <view class="title_2">收</view>
          <view class="value">
            <view class="name">{{ orderInfo.receiver }} {{ orderInfo.receiverTel }}</view>
            <view class="pos">{{ orderInfo.fromAdd || '-' }}</view>
          </view>
        </view>
      </view>
    </view>

    <view class="qh-network">
      <view class="title" @click="saveCodeNew">取货网点</view>
      <view class="content">
        <image
            class="wangdianImg"
            src="@/assets/images/index/wangdian_img_putongwangdian.png"
        ></image>
        <view class="text">
          <view class="text1">{{ orderInfo.destinationBranchName || '-' }}</view>
          <view class="text2">{{ orderInfo.destinationAddress || '-' }}</view>
          <view class="text3">距离收货地</view>
        </view>
        <view class="icon_group">
          <view @click="toDial(orderInfo)">
            <image
                src="@/assets/images/index/icon_wangdiandianhua.png"
            ></image>
          </view>
          <view @click="toMap(orderInfo)">
            <image
                style="margin-left: 5px"
                src="@/assets/images/index/icon_wangdiandaohang.png"
            ></image>
          </view>
        </view>
      </view>
    </view>

    <view class="goods-box">
      <view class="title">货物信息</view>
      <view class="goods-list">
        <view class="goods-item" v-for="(item,index) in (orderInfo.goodsInfo||[])">
          <text>{{item.goodsName}}{{item.goodsNumber}}件</text>
          <text>{{item.specifications}}</text>
        </view>
      </view>
    </view>

    <view class="bottoms">
      <view class="submit-btn" @click="submit">确定</view>
    </view>

    <date-choose :show="dateObj.show" :date="dateObj.date" :startTime="dateObj.startTime" :endTime="dateObj.endTime" @close="closeDateChoose" />


    <view style="position:fixed;left:0;top:99999999rpx">
      <canvas style="width: 702rpx;height: 1000rpx;" canvas-id="bcfhm"></canvas>
    </view>

    <dialBranch :show="showTelPopup" :tel="branchTel" :title="'拨打电话'" @close="showTelPopup=false" :show-close="true"></dialBranch>
  </view>
</template>
<script lang="ts" setup>
import { ref, computed } from "vue";
import {$get, $post, getBranchTel} from "@/@api";
import { useDidShow } from "@tarojs/taro";
import Taro, {useRouter} from "@tarojs/taro";
import dateChoose from './dateChoose.vue'
import dialBranch from "@/components/dialBranch/index"
import useUserInfoStore from "../../store/user";
const userStore = useUserInfoStore()
import dayjs from "dayjs"
const dateObj = ref<any>({
  show: false,
  date: '',
  startTime: '',
  endTime: '',
})
const formData = ref<any>({
  name: '',
  phone: '',
  idCard: '',
  startTime: '',
  endTime: '',
})
const orderInfo = ref<any>({})
const qhTime = computed(() => {
  if(!formData.value.startTime || !formData.value.endTime){
    return ''
  }
  return formData.value.startTime + ' ~ ' + formData.value.endTime
})
const openDateChoose = ()=>{
  dateObj.value.show = true
}
const closeDateChoose = (save, times)=>{
  if(save){
    formData.value.startTime  = times.date + ' ' + times.start
    formData.value.endTime  = times.date + ' ' + times.end
  }
  dateObj.value.show = false
}

const getOrderInfo = async ()=>{
  let res: any = await $post(
      "/selfmachine-system/order/list",
      {
        billNo: useRouter().params.billNo,
      }
  )
  orderInfo.value = res.data.rows[0]
}

const submit = async ()=>{
  if(!formData.value.name || !formData.value.phone || !formData.value.idCard || !formData.value.name || !formData.value.startTime || !formData.value.endTime){
    Taro.showToast({
      icon: 'none',
      title: '请完善代取人信息'
    })
    return
  }
  let res: any = await $post(
      "/selfmachine-system/order/saveReceiver",
      {
        billNo: useRouter().params.billNo,
        goodsType: orderInfo.value.goodsType,
        authorizeUser: formData.value.name,
        authorizePhone: formData.value.phone,
        authorizeId: formData.value.idCard,
        authorizeLsatTime: formData.value.startTime + '&&' + formData.value.endTime,
      }
  )
  if(res.data.code==200){
    Taro.showToast({
      icon: 'success',
      title: '提交成功'
    })
    Taro.navigateTo({
      url: '/pages/receiveAgent/code?billNo='+useRouter().params.billNo
    })
  }
}


if(dayjs(decodeURIComponent(useRouter().params.expirationTime||'')).unix() < dayjs().unix()){
  Taro.showToast({
    icon: 'error',
    title: '该链接已过期',
    duration: 2000,
    success: ()=>{
      setTimeout(()=>{
        Taro.reLaunch({
          url: "/pages/index/index",
        })
      },2000)
    }
  })
}
else if(!Taro.getStorageSync('userToken')){
  let query = `?billNo=${useRouter().params.billNo}&expirationTime=${useRouter().params.expirationTime}`
  Taro.redirectTo({
    url: `/pages/login/index?url=${encodeURIComponent('/pages/receiveAgent/index'+query)}`
  })
}
else {
  getOrderInfo()
}


//网点拨打电话
const showTelPopup = ref<boolean>(false)
const branchTel = ref<string>('')
const toDial = async (item) => {
  let res = await getBranchTel([{branchCompanyNo:item.destinationBranch}])
  if(res.code == 200 && res.data && res.data.length){
    branchTel.value = res.data[0].tel
    showTelPopup.value = true
  }
  else{
    Taro.showToast({
      icon: 'none',
      title: '该网点无联系方式'
    })
  }
}

//跳转到网点定位
function toMap(item: any) {
  $get("/selfmachine-system/branch/particulars", {
    btcId: userStore.userInfo.customerCode,
    branchCompanyNo: item.destinationBranch,
  }).then((res: any) => {
    if (res.data.code == 200) {
      Taro.openLocation({
        longitude: Number(res.data.data.localX || res.data.data.lng),
        latitude: Number( res.data.data.localY || res.data.data.lat),
        scale: 11,
      });
    }
  });
}

</script>
<style lang="scss">
page {
  background-color: #f5f5f5;
}
.ra_container {
  width: 100%;
  box-sizing: border-box;
  padding: 0rpx 24rpx 144rpx;
  .st {
    padding-left: 10rpx;
    color: #777;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 440rpx;
  }

  .address-info {
    width: 100%;
    box-sizing: border-box;
    border-radius: 16rpx;
    margin-top: 16rpx;
    overflow: hidden;
    background-color: #fff;
    padding-bottom: 32rpx;
    .head {
      width: 100%;
      height: 72rpx;
      box-sizing: border-box;
      padding: 0 24rpx;
      display: flex;
      position: relative;
      align-items: center;
      background: linear-gradient(96deg, #CCE0FF 0%, #FFFFFF 100%);
      text {
        font-size: 24rpx;
      }
      .copy {
        font-size: 18rpx;
        padding: 7rpx 12rpx;
        border-radius: 20rpx;
        border: 1rpx solid #666;
        margin-left: 12rpx;
        box-sizing: border-box;
      }
      .car {
        position: absolute;
        right: 0;
        bottom: 0;
        width: 158rpx;
        height: 88rpx;
      }
    }
    .sends {
      width: 100%;
      .row {
        width: 100%;
        display: flex;
        align-items: center;
        font-weight: 500;
        box-sizing: border-box;
        padding: 0 24rpx 0 27rpx;
        .title_1 {
          width: 42rpx;
          height: 42rpx;
          line-height: 42rpx;
          text-align: center;
          background: linear-gradient(150deg, #3783FF 14%, #125AD2 93%);
          color: #fff;
          font-size: 20rpx;
          border-radius: 50%;
        }
        .title_2 {
          width: 42rpx;
          height: 42rpx;
          line-height: 42rpx;
          text-align: center;
          background: linear-gradient(137deg, #13E879 14%, #0EC365 89%);
          color: #fff;
          font-size: 20rpx;
          border-radius: 50%;
        }
        .value {
          width: calc(100% - 60rpx);
          margin-left: 16rpx;
          display: flex;
          flex-direction: column;
          padding: 12rpx 0;
          .name {
            width: 100%;
            font-size: 32rpx;
            line-height: 48rpx;
            color: #252525;
          }
          .pos {
            width: 100%;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            color: #666;
            font-size: 24rpx;
            line-height: 32rpx;
          }
        }
      }
    }
  }

  .qh-network {
    width: 100%;
    background-color: #fff;
    border-radius: 16rpx;
    box-sizing: border-box;
    padding: 0 24rpx;
    margin-top: 16rpx;
    .title {
      height: 96rpx;
      line-height: 96rpx;
      width: 100%;
      border-bottom: 1rpx solid #f5f5f5;
      font-size: 32rpx;
      font-weight: 500;
      color: #252525;
    }
    .content {
      width: 100%;
      height: 160rpx;
      position: relative;
      box-sizing: border-box;
      display: flex;
      align-items: center;

      .wangdianImg {
        width: 112rpx;
        height: 112rpx;
        margin-right: 16rpx;
      }
      .text {
        height: 112rpx;
        width: 300rpx;
        .text1 {
          font-weight: 700;
          font-size: 28rpx;
          color: #252525;
        }
        .text2 {
          font-size: 22rpx;
          color: #666666;
          margin-bottom: 6rpx;
          line-height: 30rpx;
          width: 300rpx;
          height: 30rpx;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .text3 {
          font-size: 22rpx;
          color: #666666;
        }
      }
      .icon_group {
        display: flex;
        height: 100%;
        align-items: center;
        margin-left: 100rpx;
        image {
          width: 64rpx;
          height: 64rpx;
        }
      }
    }
  }

  .goods-box {
    width: 100%;
    background-color: #fff;
    border-radius: 16rpx;
    box-sizing: border-box;
    padding: 0 24rpx 24rpx;
    margin-top: 16rpx;
    .title {
      height: 96rpx;
      line-height: 96rpx;
      width: 100%;
      font-size: 32rpx;
      font-weight: 500;
      color: #252525;
    }
    .goods-list {
      width: 100%;
      background-color: #f5f5f5;
      border-radius: 16rpx;
      box-sizing: border-box;
      .goods-item {
        width: 100%;
        height: 88rpx;
        padding: 0 24rpx;
        box-sizing: border-box;
        border-bottom: 1rpx solid #eaeaea;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #252525;
        font-size: 26rpx;
        &:last-child {
          border-bottom: none;
        }
      }
    }
  }

  .bottoms {
    position: fixed;
    width: 100%;
    height: 128rpx;
    left: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    border-top: 1rpx solid #f5f5f5;
    .submit-btn {
      width: 700rpx;
      height: 88rpx;
      background-color: #0966ff;
      color: #fff;
      font-size: 32rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 44rpx;
    }
  }
}
</style>
